﻿using UnityEngine;
using System.Collections;

public class Element : MonoBehaviour {

    public bool IsMine;
    public Sprite[] EmptyTextures;
    public Sprite MineTexture;

    void Start() {
        
    }

    public void LoadTexture( int adjacentCount ) {
        GetComponent<SpriteRenderer>().sprite = IsMine ? MineTexture : EmptyTextures[adjacentCount];
    }

    public bool isCovered() {
        return GetComponent<SpriteRenderer>().sprite.texture.name == "bloc";
    }

    void OnMouseUpAsButton() {
        if ( IsMine ) {
            Grid.uncoverMines();

            print( "you lose" );
        } else {
            int x = (int)transform.localPosition.x;
            int y = (int)transform.localPosition.y;
            LoadTexture( Grid.adjacentMines( x, y ) );

            Grid.FFuncover( x, y, new bool[Grid.W, Grid.H] );

            if ( Grid.IsFinished() ) {
                print( "you win" );
            }
        }
    }
}
